import { expect, test, describe } from 'vitest'
import { build } from '../src/server/app.js'
import { TEST_CONNECTION_STRING } from './lib/utils.js'

describe('server/routes/config', () => {
  test('should list config with query parameters', async () => {
    const app = build()
    const response = await app.inject({
      method: 'GET',
      url: '/config?limit=5&offset=0',
      headers: {
        pg: TEST_CONNECTION_STRING,
      },
    })
    expect(response.statusCode).toBe(200)
    expect(response.json()).toMatchInlineSnapshot(`
      [
        {
          "boot_val": "on",
          "category": "Autovacuum",
          "context": "sighup",
          "enumvals": null,
          "extra_desc": null,
          "group": "Autovacuum",
          "max_val": null,
          "min_val": null,
          "name": "autovacuum",
          "pending_restart": false,
          "reset_val": "on",
          "setting": "on",
          "short_desc": "Starts the autovacuum subprocess.",
          "source": "default",
          "sourcefile": null,
          "sourceline": null,
          "subgroup": "",
          "unit": null,
          "vartype": "bool",
        },
        {
          "boot_val": "0.1",
          "category": "Autovacuum",
          "context": "sighup",
          "enumvals": null,
          "extra_desc": null,
          "group": "Autovacuum",
          "max_val": "100",
          "min_val": "0",
          "name": "autovacuum_analyze_scale_factor",
          "pending_restart": false,
          "reset_val": "0.1",
          "setting": "0.1",
          "short_desc": "Number of tuple inserts, updates, or deletes prior to analyze as a fraction of reltuples.",
          "source": "default",
          "sourcefile": null,
          "sourceline": null,
          "subgroup": "",
          "unit": null,
          "vartype": "real",
        },
        {
          "boot_val": "50",
          "category": "Autovacuum",
          "context": "sighup",
          "enumvals": null,
          "extra_desc": null,
          "group": "Autovacuum",
          "max_val": "2147483647",
          "min_val": "0",
          "name": "autovacuum_analyze_threshold",
          "pending_restart": false,
          "reset_val": "50",
          "setting": "50",
          "short_desc": "Minimum number of tuple inserts, updates, or deletes prior to analyze.",
          "source": "default",
          "sourcefile": null,
          "sourceline": null,
          "subgroup": "",
          "unit": null,
          "vartype": "integer",
        },
        {
          "boot_val": "200000000",
          "category": "Autovacuum",
          "context": "postmaster",
          "enumvals": null,
          "extra_desc": null,
          "group": "Autovacuum",
          "max_val": "2000000000",
          "min_val": "100000",
          "name": "autovacuum_freeze_max_age",
          "pending_restart": false,
          "reset_val": "200000000",
          "setting": "200000000",
          "short_desc": "Age at which to autovacuum a table to prevent transaction ID wraparound.",
          "source": "default",
          "sourcefile": null,
          "sourceline": null,
          "subgroup": "",
          "unit": null,
          "vartype": "integer",
        },
        {
          "boot_val": "3",
          "category": "Autovacuum",
          "context": "postmaster",
          "enumvals": null,
          "extra_desc": null,
          "group": "Autovacuum",
          "max_val": "262143",
          "min_val": "1",
          "name": "autovacuum_max_workers",
          "pending_restart": false,
          "reset_val": "3",
          "setting": "3",
          "short_desc": "Sets the maximum number of simultaneously running autovacuum worker processes.",
          "source": "default",
          "sourcefile": null,
          "sourceline": null,
          "subgroup": "",
          "unit": null,
          "vartype": "integer",
        },
      ]
    `)
    await app.close()
  })
})
